package fun.sssdnsy.controller;

import fun.sssdnsy.model.PasswordEntry;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

public class ButtonTableCell implements Callback<TableColumn<PasswordEntry, Void>, TableCell<PasswordEntry, Void>> {

    @Override
    public TableCell<PasswordEntry, Void> call(TableColumn<PasswordEntry, Void> param) {
        return new TableCell() {
            private final Button button = new Button("删除");
            private boolean isDeleted = false;

            {
                button.getStyleClass().add("action-button");
                button.setOnAction(event -> {
                    if (!isDeleted) {
                        // TODO: 添加人脸识别验证
                        getTableRow().getStyleClass().add("deleted-row");
                        button.setText("撤销");
                        isDeleted = true;
                    } else {
                        getTableRow().getStyleClass().remove("deleted-row");
                        button.setText("删除");
                        isDeleted = false;
                    }
                });
            }

            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    setGraphic(button);
                }
            }
        };
    }
}
